﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MpSoft.Library.BUSS.Customers;
using MpSoft.Library.DTO;
using MpSoft.Library.BUSS.Settings;
using MpSoft.Library.BUSS.Products;

namespace MpSoft.Web.AdminCP.Controls.Customer
{
    public partial class OrderEdit : System.Web.UI.UserControl
    {
        #region Var

        private int orderID = -1;
        private Order order = null;
        private ShippingMethod shipping = null;
        private PaymentMethod payment = null;
        private double totalcart = 0;

        #endregion

        #region Method

        private void BindControl()
        {
            if (orderID > 0)
            {
                order = OrderBUSS.Order_ByID(orderID);
                if (order.IsActive)
                    chkIsActive.Checked = true;
                shipping = ShippingMethodBUSS.ShippingMethod_ByID(order.ShippingMethodID);
                payment = PaymentMethodBUSS.PaymentMethod_ByID(order.PaymentMethodID);
                BindListCart();
            }
        }

        private int Save()
        {
            if (orderID > 0)
            {
                order = OrderBUSS.Order_ByID(orderID);
                order.IsActive = chkIsActive.Checked;
                return OrderBUSS.Order_Update(order);
            }
            return -1;
        }

        private void Message(string message, int type)
        {
            switch (type)
            {
                case 1:
                    MpSoft.Basic.Utils.CommonHelper.ResetForm(this.Controls);
                    ltlMessage.Text = "<p class='message-success'><i class='glyphicon-ok'></i> " + message + "</p>";
                    break;
                case 2:
                    ltlMessage.Text = "<p class='message-warning'><i class='glyphicon-warning_sign'></i> " + message + "</p>";
                    break;
            }
        }

        private void BindListCart()
        {
            var listcart = LoadCart();
            if (listcart != null)
                totalcart = OrderBUSS.GetTotalOrder(listcart);
            rplist.DataSource = listcart;
            rplist.DataBind();

        }
        //Load Product of Cart
        private List<Cart> LoadCart()
        {
            var list = OrderProductBUSS.OrderProduct_Select(-1, -1, order.OrderID);
            if (list != null)
            {
                List<Cart> listCart = new List<Cart>();
                foreach (var obj in list)
                {
                    var cart = new Cart();
                    var product = ProductBUSS.Product_ByID(obj.ProductID);
                    cart.Quantily = obj.Quantily;
                    cart.FormatUrl = product.FormatUrl;
                    cart.ProductID = obj.ProductID;
                    cart.ProductName = product.ProductName;
                    cart.Images = product.Images;
                    listCart.Add(cart);
                }
                return listCart;
            }
            return null;
        }

        #endregion

        #region Event

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["itemid"] != null)
                int.TryParse(Request.QueryString["itemid"].ToString(), out orderID);
            if (!IsPostBack)
                BindControl();
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            if (Save() > 0)
                Message("Cập nhật thành công!", 1);
        }
        protected void cmdSaveExit_Click(object sender, EventArgs e)
        {
            if (Save() > 0)
                Response.Redirect("?mod=order");
        }
        #endregion

        #region properties

        public int OrderID { get { return orderID; } }
        public Order Order { get { return order; } }
        public ShippingMethod ShippingMethod { get { return shipping; } }
        public PaymentMethod PaymentMethod { get { return payment; } }
        public double TotalCart { get { return totalcart; } }

        #endregion

    }
}